var _tools = require("../../../utils/tools.js"), _tools2 = _interopRequireDefault(_tools);

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

var that, app = getApp();

function compareVersion(t, a) {
    t = t.split("."), a = a.split(".");
    for (var e = Math.max(t.length, a.length); t.length < e; ) t.push("0");
    for (;a.length < e; ) a.push("0");
    for (var n = 0; n < e; n++) {
        var i = parseInt(t[n]), r = parseInt(a[n]);
        if (r < i) return 1;
        if (i < r) return -1;
    }
    return 0;
}

Page({
    data: {
        ad_time: "10",
        full_screen_ad: "",
        setTimeself: "",
        type: 1,
        parent_id: "",
        id: ""
    },
    onLoad: function(t) {
        if (t.parent_id && (wx.setStorageSync("parent_id", t.parent_id), this.data.parent_id = t.parent_id), 
        t.type && (this.data.type = t.type), t.id && (this.data.id = t.id), (that = this).clearTime(), 
        0 <= compareVersion(wx.getSystemInfoSync().SDKVersion, "2.10.4")) {
            var a = {
                action: "loading",
                contr: "noLogin",
                token: wx.getStorageSync("token")
            };
            _tools2.default.request(a, function(t) {
                that.setData(t.info), t.info.ad_time && t.info.full_screen_ad ? that.countdown() : that.reLaun();
            });
        } else that.reLaun();
    },
    clearTime: function() {
        clearTimeout(that.data.setTimeself);
    },
    countdown: function() {
        that.data.ad_time--, that.data.ad_time < 0 ? (that.reLaun(), that.clearTime()) : that.data.setTimeself = setTimeout(function() {
            that.setData({
                ad_time: that.data.ad_time
            }), that.countdown();
        }, 1e3);
    },
    isReLaun: function() {
        this.data.ad_time < 1 && this.reLaun();
    },
    reLaun: function() {
        1 == this.data.type ? wx.reLaunch({
            url: "/bh_bargain/pages/index/index?parent_id=" + this.data.parent_id
        }) : wx.reLaunch({
            url: "/bh_bargain/pages/bargain/bargain?parent_id=" + this.data.parent_id + "&id=" + this.data.id
        });
    },
    onUnload: function() {
        this.clearTime();
    },
    adLoad: function() {
        console.log("原生模板广告加载成功");
    },
    adError: function(t) {
        console.log("原生模板广告加载失败", t), that.reLaun();
    }
});